package com.zz.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zz.entity.Excel;
import com.zz.entity.Sections;
import com.zz.entity.Teachers;
import com.zz.entity.TeachersSearch;
import com.zz.service.TeachersService;

@RestController
public class TeachersController {
	@Autowired
	TeachersService teachersService;
	/**
	 * zfp
	 * 查所有教师
	 * @return
	 */
	@RequestMapping("findAllTeach")
	public List<Teachers> findAll(){
		List<Teachers> tt=teachersService.findAll();
		return tt;
	}
	/**
	 * zfp
	 * 分页多条件
	 * @return
	 */
	@RequestMapping("/X-admin/findTeachers")
	public Object findTeachers(TeachersSearch t,Integer page,Integer limit) {
		
		int count = teachersService.count();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", teachersService.find(t, page, limit).getContent());
		map.put("count", count);
		return map;
	}
	//添加
	@RequestMapping("/X-admin/addTeachers")
	public Object addTeachers(Teachers t, Sections s) {
		try {
			t.setSections(s);
			teachersService.save(t, s);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}
	//修改
	@RequestMapping("/X-admin/upTeachers")
	public Object upTeachers(Teachers t, Sections s) {
		try {
			t.setSections(s);
			teachersService.save(t, s);
			return 1;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}
	//删除
	@RequestMapping("/X-admin/delTeachers")
	public Integer delTeachers(Integer id) {
		try {
			teachersService.delete(id);
			return 1;
		} catch (Exception e) {
			return 0;
		}
		
	}
	//导入表格
	@RequestMapping("/X-admin/ExTeachers")
	public String EStu(MultipartFile file) throws Exception {
	
	teachersService.getExcelTeachers(file);
	  return "上传成功";
  }
	//导出
	@RequestMapping("/X-admin/OutTeachers")
	 public void OutTeachers(HttpServletResponse response) throws IOException {
	        HSSFWorkbook workbook = new HSSFWorkbook();
	        HSSFSheet sheet = workbook.createSheet("信息表");

	        List<Teachers> classmateList = teachersService.findAll();

	        String fileName = "sheet"  + ".xls";//设置要导出的文件的名字
	        //新增数据行，并且设置单元格数据

	        int rowNum = 1;

	        String[] headers = { "卡号", "姓名", "性别", "状态"};
	        //headers表示excel表中第一行的表头

	        HSSFRow row = sheet.createRow(0);
	        //在excel表中添加表头

	        for(int i=0;i<headers.length;i++){
	            HSSFCell cell = row.createCell(i);
	            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
	            cell.setCellValue(text);
	        }

	        //在表中存放查询到的数据放入对应的列
	        for (Teachers teach : classmateList) {
	            HSSFRow row1 = sheet.createRow(rowNum);
	            row1.createCell(0).setCellValue(teach.getCardno());
	            row1.createCell(1).setCellValue(teach.getName());
	            row1.createCell(2).setCellValue(teach.getSex());
	            row1.createCell(3).setCellValue(teach.getStatus());
	            rowNum++;
	        }

	        response.setContentType("application/octet-stream");
	        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
	        response.flushBuffer();
	        workbook.write(response.getOutputStream());
	 }

}
